﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;

namespace WebApplication.Admin
{
    public partial class ChiTietHoaDonXuatList : System.Web.UI.Page
    {
        CHUCNANG objchucnang = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            KiemtraQUyenSuDung();
            if (!IsPostBack)
            {
                LoadHienthi();
            }

        }
        public void LoadHienthi()
        {
            IUnitOfWork iunfwork = new EFUnitOfWork();
            CHITIETHOADONXUATRepository repository = new CHITIETHOADONXUATRepository(new EFRepository<CHITIETHOADONXUAT>(), iunfwork);
            IQueryable<CHITIETHOADONXUAT> objChutietHdxuat = repository.All();
            grHoadonxuat.DataSource = objChutietHdxuat;
            grHoadonxuat.DataBind();

        }

        protected void grHoadonxuat_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbtnsua")
            {
                IUnitOfWork iunitfwork = new EFUnitOfWork();
                CHITIETHOADONXUATRepository repository = new CHITIETHOADONXUATRepository(new EFRepository<CHITIETHOADONXUAT>(), iunitfwork);
                string mahdx = e.CommandArgument.ToString();
                DateTime dt = Commomcs.convertDate(DateTime.Now.ToString("dd/MM/yyyy"));
                CHITIETHOADONXUAT objChitietHDX = repository.All().Where(p => p.MAHDX == mahdx).FirstOrDefault();
                if (objChitietHDX != null)
                {
                    if (objChitietHDX.HOADONXUAT.NGAYXUAT.Equals(dt))
                    {
                        Response.Redirect("ChiTietHoaDonXuatAdd.aspx?MAHDX=" + e.CommandArgument);
                    }
                    else
                    {
                        RegisterClientScriptBlock("Thông báo", "<script> alert('Chỉ được sửa những hóa đơn xuất trong ngày')</script>");
                    }
                }
            }
            if (e.CommandName == "lbtnxoa")
            {
                try
                {
                    IUnitOfWork iunitfwork = new EFUnitOfWork();
                    CHITIETHOADONXUATRepository repository = new CHITIETHOADONXUATRepository(new EFRepository<CHITIETHOADONXUAT>(), iunitfwork);
                    CHITIETHOADONXUAT objchitiet = repository.GetById(e.CommandArgument.ToString());
                    repository.Delete(objchitiet);
                    repository.Save();
                    HOADONXUATRepository repositoryHDX = new HOADONXUATRepository(new EFRepository<HOADONXUAT>(), iunitfwork);
                    HOADONXUAT objHDX = repositoryHDX.GetById(e.CommandArgument.ToString());
                    repositoryHDX.Delete(objHDX);
                    repositoryHDX.Save();
                    LoadHienthi();
                }
                catch (Exception ex)
                {
                    RegisterClientScriptBlock("Thông báo", "<script> alert('Không thể xóa được')</script>");
                }
            }
        }
        public void KiemtraQUyenSuDung()
        {
            lbtnthemmoi.Visible = false;
            if (Session["MANV"] != null)
            {
                string strMANV = Session["MANV"].ToString();
                IUnitOfWork iunitwork = new EFUnitOfWork();
                lbtnthemmoi.Visible = false;
                NHANVIENRepository repository = new NHANVIENRepository(new EFRepository<NHANVIEN>(), iunitwork);
                NHANVIEN objNhanvien = repository.All().Where(p => p.MANV == strMANV).FirstOrDefault();
                if (objNhanvien != null)
                {
                    objchucnang = Commomcs.QuyenTruyCapChucnang(Request.Url.AbsolutePath, objNhanvien.MANV);
                    if (objchucnang != null)
                    {
                        if (objchucnang.ISADD == true)
                        {
                            lbtnthemmoi.Visible = true;
                        }
                        else
                        {
                            lbtnthemmoi.Visible = false;
                        }

                    }
                }
            }
        }
        protected void grHoadonxuat_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex < 0) return;
            CHITIETHOADONXUAT objChitietHDx = ((CHITIETHOADONXUAT)e.Row.DataItem);
            if (objChitietHDx != null)
            {
                Label lbmathang = ((Label)e.Row.FindControl("lbmathang"));
                if (lbmathang != null)
                {
                    lbmathang.Text = objChitietHDx.MATHANG.TENMH;
                }
                Label lbkhachhang = ((Label)e.Row.FindControl("lbkhachhang"));
                if (lbkhachhang != null)
                {
                    lbkhachhang.Text = objChitietHDx.HOADONXUAT.KHACHHANG.TENKH;
                }
                Label lbnhanvien = ((Label)e.Row.FindControl("lbnhanvien"));
                if (lbnhanvien != null)
                {
                    lbnhanvien.Text = objChitietHDx.HOADONXUAT.NHANVIEN.TENNV;
                }
                Label lbtongtien = ((Label)e.Row.FindControl("lbtongtien"));
                if (lbtongtien != null)
                {
                    lbtongtien.Text = objChitietHDx.HOADONXUAT.TIENPHAITRA.ToString();
                }
                Label lbngayxuat = ((Label)e.Row.FindControl("lbngayxuat"));
                if (lbngayxuat != null)
                {
                    lbngayxuat.Text = objChitietHDx.HOADONXUAT.NGAYXUAT.ToString();
                }
                Label lbtinhtrang = ((Label)e.Row.FindControl("lbtinhtrang"));
                if (lbtinhtrang != null)
                {
                    if (objChitietHDx.TINHTRANG == true)
                    {
                        lbtinhtrang.Text = "Đã thanh toán";
                    }
                    else
                    {
                        lbtinhtrang.Text = "Chưa thanh toán";
                    }
                }
            }
            LinkButton lbtnsua = ((LinkButton)e.Row.FindControl("lbtnsua"));
            LinkButton lbtnxoa = ((LinkButton)e.Row.FindControl("lbtnxoa"));
            lbtnsua.Visible = false;
            lbtnxoa.Visible = false;
            if (objchucnang != null)
            {
                lbtnsua.Visible = false;
                if (lbtnsua != null)
                {
                    if (objchucnang.ISEDIT == true)
                    {
                        lbtnsua.Visible = true;
                    }
                    else
                    {
                        lbtnsua.Visible = false;
                    }
                }
                lbtnxoa.Visible = false;
                if (lbtnxoa != null)
                {
                    if (objchucnang.ISDELETE == true)
                    {
                        lbtnxoa.Visible = true;
                    }
                    else
                    {
                        lbtnxoa.Visible = false;
                    }
                }
            }
        }

        protected void lbtnthemmoi_Click(object sender, EventArgs e)
        {
            Response.Redirect("ChiTietHoaDonXuatAdd.aspx");
        }

        protected void ltbntimkiem_Click(object sender, EventArgs e)
        {
            TimKiem();
        }
        public void TimKiem()
        {
            if (string.IsNullOrEmpty(txtmahd.Value) && string.IsNullOrEmpty(txttenkh.Value))
            {
                RegisterClientScriptBlock("Thông báo", "<script> alert('Vui lòng chọn phần tìm kiếm')</script>");
                return;
            }
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            CHITIETHOADONXUATRepository repository = new CHITIETHOADONXUATRepository(new EFRepository<CHITIETHOADONXUAT>(), iunitfwork);
            if (!string.IsNullOrEmpty(txtmahd.Value))
            {
                IQueryable<CHITIETHOADONXUAT> LstHoadx = repository.All().Where(p => p.MAHDX.Contains(txtmahd.Value));
                grHoadonxuat.DataSource = LstHoadx;
                grHoadonxuat.DataBind();
            }
            if (!string.IsNullOrEmpty(txttenkh.Value))
            {
                IQueryable<CHITIETHOADONXUAT> LstHoadx = repository.All().Where(p =>p.HOADONXUAT.KHACHHANG.TENKH.Contains(txttenkh.Value));
                grHoadonxuat.DataSource = LstHoadx;
                grHoadonxuat.DataBind();
            }
            if (!string.IsNullOrEmpty(txtmahd.Value) && ! string.IsNullOrEmpty(txttenkh.Value))
            {
                IQueryable<CHITIETHOADONXUAT> LstHoadx = repository.All().Where(p => p.MAHDX.Contains(txtmahd.Value)&& p.MAHDX.Contains(txtmahd.Value));
                grHoadonxuat.DataSource = LstHoadx;
                grHoadonxuat.DataBind();
            }
        }

        protected void grHoadonxuat_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            LoadHienthi();
            grHoadonxuat.PageIndex = e.NewSelectedIndex;
            grHoadonxuat.DataBind();

        }
    }
}